package com.rm.zeyou.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.entity.UserBack;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.UserBackMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.UserBackService;
import com.rm.zeyou.util.back.BankCardUtils;
import com.rm.zeyou.util.back.GetColor;
import com.rm.zeyou.util.back.vo.GetCardDetailVo;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.AddTechnicianBackVo;
import com.rm.zeyou.vo.response.SelectTechnicianBackResponseVo;
import com.rm.zeyou.vo.response.TechnicianBackResponseVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 用户银行卡表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:09
 */
@Service
public class UserBackServiceImpl extends ServiceImpl<UserBackMapper, UserBack> implements UserBackService {
    @Resource
    private UserBackMapper userBackMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> addUserBack(Long userId, AddTechnicianBackVo addVo) {
        BankCardUtils.checkBankCard(addVo.getBackldCard());
        //查询银行卡有没有绑定
        Integer isBindBack = userBackMapper.selectIsBindBack(addVo.getBackldCard());
        if (isBindBack > 0) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.BACK_IS_EXIST);
        }
        GetCardDetailVo cardDetail = BankCardUtils.getCardDetail(addVo.getBackldCard());
        if (ObjectUtils.isEmpty(cardDetail)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.BACK_ERROR);
        }
        String typeOfBank = BankCardUtils.getTypeOfBank(cardDetail.getCardType());
        UserBack insertUserBack = new UserBack();
        insertUserBack.setUserId(userId);
        insertUserBack.setBackIdcard(addVo.getBackldCard());
        insertUserBack.setBackBranch(addVo.getBackBranch());
        insertUserBack.setBackUserName(addVo.getBackUserName());
        insertUserBack.setBackName(addVo.getBackName() + typeOfBank);
        insertUserBack.setBackLogo(cardDetail.getBankImg());
        insertUserBack.setBackColor(GetColor.getSexColor(cardDetail.getBankImg()));
        int insert = userBackMapper.insert(insertUserBack);
        return ApiRestResponse.success(insert);

    }

    public static void main(String[] args) {
        GetCardDetailVo cardDetail = BankCardUtils.getCardDetail("6230941370000564233");
        String patten = "^[1-9]\\d{9,29}$";
        System.out.println(Pattern.matches(patten, "6230941370000564234"));
        System.out.println("cardDetail = " + JSON.toJSONString(cardDetail));
        String typeOfBank = BankCardUtils.getTypeOfBank(cardDetail.getCardType());
        System.out.println("typeOfBank = " + typeOfBank);
    }

    @Override
    public ApiRestResponse<SelectTechnicianBackResponseVo> selectUserBackList(Long userId) {
        SelectTechnicianBackResponseVo vo = new SelectTechnicianBackResponseVo();
        List<TechnicianBackResponseVo> technicianBacks = userBackMapper.selectUserBackList(userId);
        if (!CheckEmptyUtil.isEmpty(technicianBacks)) {
            technicianBacks.forEach(item -> {
                item.setBackIdcard(DesensitizedUtil.bankCard(item.getBackIdcard()));
            });
            vo.setBackList(technicianBacks);
            vo.setBackCount(technicianBacks.size());
            return ApiRestResponse.success(vo);
        }
        vo.setBackCount(technicianBacks.size());
        return ApiRestResponse.success(vo);
    }
}
